import VueRouter from 'vue-router'
import Vue from 'vue'
import Mine from '../views/Mine.vue'
import Index from '../views/Index.vue'
import Discover from '../views/Discover.vue'
import Goods from '../views/Goods.vue'
import Cart from '../views/Cart.vue'
import Search from '../views/Search.vue'
import Regist from '../views/Regist.vue'
import Address from '../views/Address.vue'
import AddressEdit from '../views/AddressEdit.vue'
import Order from '../views/Order.vue'
import Login from '../views/Login.vue'
import More from '../views/More.vue'
import SubOrder from '../views/SubOrder.vue'


Vue.use(VueRouter)
const router = new VueRouter({
    routes: [{
        path: '/index',
        component: Index,
        name: 'Index'
    },
    // 重定向：/ -> /home
    {
        path: '/',
        redirect: '/index',
        // redirect:{name:'Home'},

    },
    {
        path: '/discover',
        component: Discover,
        name: 'Discover'
    },
    {
        path: '/cart',
        component: Cart,
        name: 'Cart',
        // 路由元信息：requiresAuth指定当前路由需要登录权限才能访问
        meta: { requiresAuth: true }
    },
    {
        path: '/mine',
        component: Mine,
        name: 'Mine',
        // 路由元信息：requiresAuth指定当前路由需要登录权限才能访问
        meta: { requiresAuth: true }
    },
    {
        path: '/search',
        component: Search,
        name: 'Search'
    },
    {
        path: '/goods/:id',
        component: Goods,
        name: 'Goods'
    },
    {
        path: '/login',
        component: Login,
        name: 'Login'
    },
    {
        path: '/reg',
        component: Regist,
        name: 'Regist'
    },
    {
        path: '/address',
        component: Address,
        name: 'Address'
    },
    {
        path: '/addressEdit',
        component: AddressEdit,
        name: 'AddressEdit'
    },
    {
        path: '/order',
        component: Order,
        name: 'Order'
    },
    {
        path: '/subOrder',
        component: SubOrder,
        name: 'SubOrder'
    },
    {
        path: '/more/:type',
        component: More,
        name: 'More'
    },
    ]
})

//全局路由守卫 判断是否登录 登录且有token放行
router.beforeEach(function (to, from, next) {
    if (to.meta.requiresAuth) {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        // console.log(userInfo.token);

        //判断登录
        if (userInfo && userInfo.token) {

            next()
            //验证token有效性
            router.app.$request.post('/tokenCheck', {
                username: userInfo.username,
            }, {
                headers: { token: userInfo.token },
            }

            ).then(({ data }) => {
                if (data.status !== 200) {
                    localStorage.removeItem('userInfo')
                    router.app.$notify({ type: "danger", message: "token失效，请重新登录" })
                    router.push({
                        path: '/login',
                        query: {
                            targetUrl: to.fullPath
                        }
                    })

                }
            })
        } else {
            next()
            router.push({
                path: '/login',
                query: {
                    targetUrl: to.fullPath
                }
            })
        }
    } else {
        next()
    }
})




export default router